package com.tester.yaa.exam001.agent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Aduthor yinanan
 * @ClassName PersonAgent
 * @Date 2022/12/4 16:43
 * @Description 采用静态代理
 **/
public class PersonAgent implements Person {

    private Person target;

    public PersonAgent(Person target) {
        this.target = target;
    }
    
    /** 
    * @Description: //实现接口
    * @author: yinanan
    * @Param: [message]
    * @return: java.lang.String  
    * @Date: 2022/12/4
    */ 
    @Override
    public String say(String message) {
        return message;
    }

    /** 
    * @Description: //动态代理--生成代理对象
    * @author: yinanan
    * @Param: []
    * @return: java.lang.Object  
    * @Date: 2022/12/4
    */ 
    public Object getProxyInstance(String str) {
        //返回一个代理对象
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
        new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return str;
            }
        });
    }
}
